package com.nothing5lose.iwillbeback;

import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.*;


public class MainActivity extends FragmentActivity {
    private GoogleMap mMap;

    boolean mNeedMyLocation = true;
    Object mLock = new Object();

    private LatLng des = new LatLng(21.024905,105.859966);

    private GoogleMap.OnMapClickListener mOnMapClickListener = new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            if(null == mMap){
                return;
            }

            //Create new Maker
            MarkerOptions mo = new MarkerOptions()
                    .position(latLng)
//                    .title("End")
//                    .snippet("TEST")
//                .draggable(true)
//                .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
                    ;

            //Add to the map
            mMap.addMarker(mo);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //No title
//        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

        //Set main layout
        setContentView(R.layout.main);

        //Setup something
        inititalSetUp();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.map_view_tab_menu, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.my_location:{
                Toast.makeText(this,"My Location",Toast.LENGTH_SHORT).show();
                //Animate camera
                mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(mMap.getMyLocation().getLatitude(),
                        mMap.getMyLocation().getLongitude())));
                break;
            }
            case R.id.change_map_type:{
                Toast.makeText(this,"Change Type",Toast.LENGTH_SHORT).show();
                //Set map type
                mMap.setMapType(mMap.getMapType() == GoogleMap.MAP_TYPE_NORMAL ? GoogleMap.MAP_TYPE_HYBRID:GoogleMap.MAP_TYPE_NORMAL);
                break;
            }
            case R.id.view_event:{
                Toast.makeText(this,"View Event",Toast.LENGTH_SHORT).show();
                break;
            }
            default:
                break;

        };
        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onResume() {
        super.onResume();

    }

    private void inititalSetUp() {
        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        }
        if (mMap == null) {
            return;
        }
//        Initialize map options. For nothing5lose:
//        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setMyLocationButtonEnabled(false);
        mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange(Location location) {
                synchronized (mLock){
                    if(mNeedMyLocation){
                        if (mMap == null) {
                            return;
                        }
                        if (null != location){

                            //My LocationLatLng
                            final LatLng myLocationLatLng = new LatLng(
                                    location.getLatitude(),
                                    location.getLongitude());

                            //Animate camera
                            mMap.animateCamera(
                                    CameraUpdateFactory.newLatLng(myLocationLatLng));
//                            new GetNavigationTask(getApplicationContext(),mMap)
//                                    .execute(
//                                            new Pair<LatLng, LatLng>(myLocationLatLng, des));
                            mNeedMyLocation = false;
                        }
                    }
                }
            }
        });
        mMap.setOnMapClickListener(mOnMapClickListener);
    }
}
